﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ParallelArrays;
using FPA = Microsoft.ParallelArrays.FloatParallelArray;
using BPA = Microsoft.ParallelArrays.BoolParallelArray;
using IPA = Microsoft.ParallelArrays.IntParallelArray;
using PA = Microsoft.ParallelArrays.ParallelArrays;

namespace CoreCalc.SheetDefinedFunctions.Accellerator
{
    
    public class CondAccNode : AccNode
    {
        public CompareNode comp;
        public AccNode TrueLeaf;
        public AccNode FalseLeaf;

        public CondAccNode(CompareNode c, AccNode TrueLeaf, AccNode FalseLeaf)
        {
            comp = c;

            this.TrueLeaf = TrueLeaf;
            this.FalseLeaf = FalseLeaf;            
        }

        public override FPA GenerateFPA(AccInputInfo info, int CallID)
        {
            return PA.Cond((BoolParallelArray)comp.GenerateBPA(info, CallID), TrueLeaf.GenerateFPAWithCache(info, CallID), FalseLeaf.GenerateFPAWithCache(info, CallID));
        }


        public class CompareNode
        {
            public AccNode v1, v2;
            public enum Type
            {
                EQ, GT, GE, LT, LE, NQ
            }
            Type type;

            public CompareNode(Type type, AccNode v1, AccNode v2)
            {
                this.type = type;
                this.v1 = v1;
                this.v2 = v2;
            }

            public Microsoft.ParallelArrays.ParallelArray GenerateBPA(AccInputInfo info, int CallID)
            {
                switch(type) {
                    case Type.EQ:
                        return PA.CompareEqual(v1.GenerateFPAWithCache(info, CallID), v2.GenerateFPAWithCache(info, CallID));
                    case Type.GT:
                        return PA.CompareGreater(v1.GenerateFPAWithCache(info, CallID), v2.GenerateFPAWithCache(info, CallID));
                    case Type.GE:
                        return PA.CompareGreaterEqual(v1.GenerateFPAWithCache(info, CallID), v2.GenerateFPAWithCache(info, CallID));
                    case Type.LT:
                        return PA.CompareLess(v1.GenerateFPAWithCache(info, CallID), v2.GenerateFPAWithCache(info, CallID));
                    case Type.LE:
                        return PA.CompareLessEqual(v1.GenerateFPAWithCache(info, CallID), v2.GenerateFPAWithCache(info, CallID));
                    case Type.NQ:
                        return PA.CompareNotEqual(v1.GenerateFPAWithCache(info, CallID), v2.GenerateFPAWithCache(info, CallID));
                }
                throw new Exception("Unkwnown compare type");
            }
        }

    }
}
